#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>


static u8 content[1024];

size_t datalen = 0 ;

ssize_t read_proc(struct file *filp, char __user *buffer, size_t count,
		loff_t *offset) {
	int p = *offset;
	if (p >= datalen) {
		return 0;
	}

	if (count > datalen - p) {
		count = datalen - p;
	}
	if (copy_to_user(buffer, content + *offset, count)) {
		return -EFAULT;
	}
	*offset += count;
	return count;
}

ssize_t write_proc(struct file *file, const char __user *buf, size_t count,
		loff_t *offset) {
	if(copy_from_user(content, buf + *offset, count)){
		pr_info("copy_from_user  error  ");
		return -EFAULT;
	}
	*offset += count;
	datalen = count;
	pr_info("content = %s\n",content);
	return count;
}

static const struct file_operations proc_ops = { .owner = THIS_MODULE, .read =
		read_proc, .write = write_proc, };

static struct proc_dir_entry *my_proc_dir = NULL;

int myinit(void) {
	my_proc_dir = proc_create("myproc", 0666, NULL, &proc_ops);
	return 0;
}

static __init int my_init(void) {
	return myinit();
}

static void __exit my_exit(void) {
	proc_remove(my_proc_dir);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("a sample driver");
